package 手写HashMap;


/**
 * @Author tyy
 * @Description TODO
 * @Since 2021/10/16
 */
public class HashMap<K, V> implements Map<K, V> {
    Entry<K, V>[] table;
    int size = 0;
    public HashMap(){
        table = new Entry[16];
    }
    @Override
    public V put(K k, V v) {
        int index = hash(k);  //取模
        Entry entry = table[index];
        if (entry == null) {
            table[index] = new Entry<K, V>(k, v, index, null);
            size++;

        }else{ // 有冲突
            table[index] = new Entry<K, V>(k, v, index, entry);
        }
        return table[index].getValue();
    }

    private int hash(K k) {
        int i = k.hashCode() % 16;
        return i > 0 ? i : -i;
    }
    @Override
    public V get(K k) {
        if (size == 0) {
            return null;
        }
        int index = hash(k);
        Entry<K, V> entry = table[index];
        return entry == null ? null : entry.getValue();
    }
    private Entry<K, V> findValue(K k, Entry<K,V> entry){
        if (k.equals(entry.getKey()) || k==entry.getKey()) {




        }
        return null;

    }

    class Entry<K, V> implements Map.Entry<K, V>{
        K k;
        V v;
        Entry next;

        public Entry(K k, V v, int index, Entry next) {
            this.k = k;
            this.v = v;
            this.next = next;
        }

        @Override
        public K getKey() {
            return k;
        }

        @Override
        public V getValue() {
            return v;
        }
    }


    public static void main(String[] args) {
        Map map = new HashMap();
        for (int i = 0; i < 10000; i++) {

            map.put("Monkey" + i, "woshilaoshi" + i);
            System.out.println(map.get("Monkey" + i));
        }
        map.put("阿白", "阿白是位好同学");
        System.out.println(map.get("阿白"));
    }
}
